﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TS2015.Models;
using System.Data.Entity;
using System.Web.Security;

namespace TS2015.Controllers
{
    public class HomeController : Controller
    {
        // GET: Home      
        public ActionResult Index()
        { 
            return View();
        }
        TS2015Context db = new TS2015Context();

        //[Authorize]
        public ActionResult Profile()
        {
            if (Session["TenDangNhap"] == null)
                return RedirectToAction("Login", "Home");

            string TenDangNhap = Session["TenDangNhap"].ToString();
            var user = db.TbNguoiDungs.Find(TenDangNhap);

            if (user == null)
                return RedirectToAction("Login", "Home");

            var thisinh = db.TbThiSinhs.Include(m => m.TbChuyenNganh).Where(m => m.MaTS == TenDangNhap).First();
            return View(thisinh);
        }

        public ActionResult Login()
        {
            HttpCookie ckMatKhau = Request.Cookies["MatKhau"];
            HttpCookie ckTenDangNhap = Request.Cookies["TenDangNhap"];
            if (ckTenDangNhap != null)
            {
                if (CheckLogin(ckTenDangNhap.Value, ckMatKhau.Value, true) )
                    return RedirectToAction("Profile", "Home");
            }
            return View();
        }

        bool CheckLogin(string TenDangNhap, string MatKhau, bool remember = false)
        {
            if (TenDangNhap != null && MatKhau != null)
            {
                string MatKhauMd5 = DuLieuChung.MaHoaMD5(MatKhau);

                var users = (from p in db.TbNguoiDungs
                             where p.TenDangNhap == TenDangNhap && p.MatKhau == MatKhauMd5
                             select p
                            );

                if (users.Count() == 0)
                {
                    ModelState.AddModelError("Error", "Tên đăng nhập hoặc mật khẩu không đúng.");
                    return false;
                }

                if (users.First().KhoaTaiKhoan == KhoaTaiKhoan.Khoa)
                {
                    ModelState.AddModelError("Error", "Tài khoản của bạn bị khóa.");
                    return false;
                }

                if (users.First().MaQuyen > 0)
                    return false;

                if (remember)
                {
                    // check if cookie exists and if yes update
                    HttpCookie ckTenDangNhap = Request.Cookies["TenDangNhap"];

                    if (ckTenDangNhap != null)
                    {
                        //force to expire it
                        ckTenDangNhap.Values.Add("TenDangNhap", users.First().TenDangNhap);
                        ckTenDangNhap.Values.Add("MatKhau", MatKhau);
                        ckTenDangNhap.Expires = DateTime.Now.AddHours(-20);
                    }
                    HttpCookie ckMatKhau = null;
 
                    //create a cookie
                    ckTenDangNhap = new HttpCookie("TenDangNhap");
                    ckMatKhau = new HttpCookie("MatKhau");

                    ckTenDangNhap.Value = TenDangNhap;
                    ckMatKhau.Value = MatKhau;

                    ckTenDangNhap.Expires = DateTime.Today.AddDays(15);
                    ckMatKhau.Expires = DateTime.Today.AddDays(15);

                    Response.Cookies.Add(ckTenDangNhap);
                    Response.Cookies.Add(ckMatKhau);
                }

                Session["TenDangNhap"] = users.First().TenDangNhap;

                return true;
            }
            return false;
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Login(string TenDangNhap, string MatKhau, bool remember = false)
        {
            if (ModelState.IsValid && TenDangNhap != null && MatKhau != null)
            {
               if( CheckLogin(TenDangNhap, MatKhau, remember))                 
                    return RedirectToAction("Profile", "Home");
            }
            return View();
        }

        [ChildActionOnly]
        //[Authorize]
        public ActionResult LeftProfile()
        {
            if (Session["TenDangNhap"] == null)
                return RedirectToAction("Login", "Common");

            string TenDangNhap = Session["TenDangNhap"].ToString();
            var user = db.TbNguoiDungs.SingleOrDefault(m=>m.TenDangNhap==TenDangNhap);

            if (user == null)
                return RedirectToAction("Login", "Common");

            var thisinh = db.TbThiSinhs.Include(m => m.TbChuyenNganh).SingleOrDefault(m => m.MaTS == TenDangNhap);

            return PartialView(thisinh);
        }
        [ChildActionOnly]
        //[Authorize]
        public ActionResult PhieuHoanThanh()
        {
            //if (Session["TenDangNhap"] == null)
            //    return RedirectToAction("Login", "Common");

            string TenDangNhap = Session["TenDangNhap"].ToString();
            var user = db.TbNguoiDungs.Find(TenDangNhap);

            if (user == null)
                return RedirectToAction("Login", "Common");

            var thisinh = db.TbThiSinhs.Include(m => m.TbChuyenNganh).Where(m => m.MaTS == TenDangNhap).First();

            return View(thisinh);
        }
        public ActionResult Logout()
        {
            Session["TenDangNhap"] = null;
 
            HttpCookie ckMatKhau = Request.Cookies["MatKhau"];
            HttpCookie ckTenDangNhap = Request.Cookies["TenDangNhap"];

            if (ckMatKhau != null)
            {
                ckMatKhau.Expires = DateTime.Now.AddDays(-1);
                Response.Cookies.Add(ckMatKhau);
            }

            if (ckTenDangNhap != null)
            {
                ckTenDangNhap.Expires = DateTime.Now.AddDays(-1);
                Response.Cookies.Add(ckTenDangNhap);
            }
            Session.Clear();
            Session.Abandon();
            Session.RemoveAll();
 
            return RedirectToAction("Index", "Home");
        }
    }
}
